import 'dart:convert';
import 'dart:io';
import 'package:animations/animations.dart';
import 'package:hibi/common/CommonUtil.dart';
import 'package:hibi/common/Config.dart';
import 'package:hibi/common/DioManager.dart';
import 'package:hibi/common/Url.dart';
import 'package:hibi/event/event_bus.dart';
import 'package:hibi/model/user_entity.dart';
import 'package:hibi/state/UserInfoState.dart';
import 'package:hibi/styles/colors.dart';
import 'package:hibi/widget/AppBarReturn.dart';
import 'package:hibi/widget/progress_dialog.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

class SafePwdPage extends StatefulWidget {
  @override
  _State createState() => new _State();
}

class _State extends State<SafePwdPage> {

  //定义变量
  Timer _timer;
  //倒计时数值
  var countdownTime = 0;
  final TextEditingController _code = new TextEditingController();
  final TextEditingController _pwd = new TextEditingController();
  final TextEditingController _pwd2 = new TextEditingController();
  final TextEditingController _pwdR = new TextEditingController();


  bool showPwd = true;
  bool showPwd2 = true;
  bool showPwd3 = true;

  UserData user = new UserData();

  @override
  void initState() {
    super.initState();
    UserData data = Provider.of<UserInfoState>(context, listen: false).userInfoModel;
    setState(() {
      user = data;
    });
  }


  @override
  void dispose() {
    super.dispose();
    if (_timer != null) {
      _timer.cancel();
    }
  }



  startCountdown() {
    countdownTime = 60;
    final call = (timer) {
      setState(() {
        if (countdownTime < 1) {
          _timer.cancel();
        } else {
          countdownTime -= 1;
        }
      });
    };
    _timer = Timer.periodic(Duration(seconds: 1), call);
  }


  void _submit(){
    if(CommonUtil.isEmpty(_pwdR.text)){
      CommonUtil.showToast('pwd_label1'.tr());
      return;
    }

    if(CommonUtil.isEmpty(_pwd.text)){
      CommonUtil.showToast('pwd_label2'.tr());
      return;
    }

    if(_pwd.text != _pwd2.text){
      CommonUtil.showToast('pwd_label4'.tr());
      return;
    }

    if(CommonUtil.isEmpty(_code.text)){
      CommonUtil.showToast('register_code_match'.tr());
      return;
    }

    _progressDialog.show();
    var params = {
      'oldPassword':CommonUtil.generateMd5(_pwdR.text),
      'code':_code.text,
      'newPassword':CommonUtil.generateMd5(_pwd.text),
    };
    DioManager.getInstance().post(Url.USER_UPDATE_PWD, params, (data){
      _progressDialog.hide();
      CommonUtil.showToast('success'.tr());
      Navigator.of(context).pop();
    }, (error){
      CommonUtil.showToast(error);
      _progressDialog.hide();
    });
  }

  void getCode(){
    if(countdownTime != 0){
      return;
    }

    _progressDialog.show();
    DioManager.getInstance().post(Url.USER_UPDATE_PWD_CODE, null, (data){
      _progressDialog.hide();
      startCountdown();
      final snackBar = SnackBar(
        content: Text('register_send_success'.tr()+':'+user.mobilePhone),
        action: SnackBarAction(
          label: 'label_undo'.tr(),
          onPressed: () {
          },
        ),);
      Scaffold.of(context).showSnackBar(snackBar);
    }, (error){
      CommonUtil.showToast(error);
      _progressDialog.hide();
    });
  }


  ProgressDialog _progressDialog;

  @override
  Widget build(BuildContext context) {
    _progressDialog = ProgressDialog(context,type: ProgressDialogType.Normal);
    _progressDialog.style(
      message: 'loading'.tr(),
      backgroundColor: Colours.dark_bg_gray,
      progressWidget: Container(
          padding: EdgeInsets.all(8.0),
          child: CircularProgressIndicator()),
      maxProgress: 100.0,
      progressTextStyle: TextStyle(
        fontSize: 13.0,
      ),
      messageTextStyle: TextStyle(
        fontSize: 19.0,
      ),
    );
    return Scaffold(
        backgroundColor: Colours.dark_bg_gray,
        appBar: AppBar(
          leading: AppBarReturn(),
        ),
        body: SingleChildScrollView(
          child: Container(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.only(top: ScreenUtil().setHeight(60)),
                  child: Text('safe_dlmm'.tr(),style: TextStyle(fontSize: ScreenUtil().setSp(38)),),
                ),
                Padding(
                  padding: EdgeInsets.only(top: ScreenUtil().setHeight(80)),
                  child: Text('password_old'.tr(),style: TextStyle(fontSize: ScreenUtil().setSp(24)),),
                ),
                Container(
                  padding: EdgeInsets.only(top: ScreenUtil().setHeight(40)),
                  child: TextField(
                    controller: _pwdR,
                    keyboardType: TextInputType.visiblePassword,
                    obscureText:showPwd,
                    autofocus: false,
                    style: TextStyle(
                      fontSize: ScreenUtil().setSp(30),
                    ),
                    decoration:InputDecoration(
                        hintText:"password_old".tr(),
                        suffixIcon: IconButton(
                          color: Colors.white,
                          splashColor: Colors.transparent,
                          highlightColor: Colors.transparent,
                          icon: ImageIcon(AssetImage(showPwd?'res/eye_close.png':'res/eye_open.png')),
                          onPressed: (){
                            setState(() {
                              showPwd = !showPwd;
                            });
                          },
                        )
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(top: ScreenUtil().setHeight(80)),
                  child: Text('password_new'.tr(),style: TextStyle(fontSize: ScreenUtil().setSp(24)),),
                ),
                Container(
                  padding: EdgeInsets.only(top: ScreenUtil().setHeight(40)),
                  child: TextField(
                    controller: _pwd,
                    keyboardType: TextInputType.visiblePassword,
                    obscureText:showPwd2,
                    autofocus: false,
                    style: TextStyle(
                      fontSize: ScreenUtil().setSp(30),
                    ),
                    decoration:InputDecoration(
                        hintText:"password_new".tr(),
                        suffixIcon:IconButton(
                          color: Colors.white,
                          splashColor: Colors.transparent,
                          highlightColor: Colors.transparent,
                          icon: ImageIcon(AssetImage(showPwd2?'res/eye_close.png':'res/eye_open.png')),
                          onPressed: (){
                            setState(() {
                              showPwd2 = !showPwd2;
                            });
                          },
                        )
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(top: ScreenUtil().setHeight(80)),
                  child: Text('passwrord_aiagin'.tr(),style: TextStyle(fontSize: ScreenUtil().setSp(24)),),
                ),
                Container(
                  padding: EdgeInsets.only(top: ScreenUtil().setHeight(40)),
                  child: TextField(
                    controller: _pwd2,
                    keyboardType: TextInputType.visiblePassword,
                    obscureText:showPwd3,
                    autofocus: false,
                    style: TextStyle(
                      fontSize: ScreenUtil().setSp(30),
                    ),
                    decoration:InputDecoration(
                        hintText:"passwrord_aiagin".tr(),
                        suffixIcon: IconButton(
                          color: Colors.white,
                          splashColor: Colors.transparent,
                          highlightColor: Colors.transparent,
                          icon: ImageIcon(AssetImage(showPwd3?'res/eye_close.png':'res/eye_open.png')),
                          onPressed: (){
                            setState(() {
                              showPwd3 = !showPwd3;
                            });
                          },
                        )
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(top: ScreenUtil().setHeight(80)),
                  child: Text('verify_mobile'.tr(),style: TextStyle(fontSize: ScreenUtil().setSp(24)),),
                ),
                Container(
                    padding: EdgeInsets.only(top: ScreenUtil().setHeight(40)),
                    child: Stack(
                      children: <Widget>[
                        TextField(
                          controller: _code,
                          autofocus: false,
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(30),
                          ),
                          decoration:InputDecoration(
                            hintText:"verify_mobile".tr(),
                            contentPadding:EdgeInsets.only(right: ScreenUtil().setWidth(150)),
                          ),
                        ),
                        Positioned(
                            right: 0,
                            bottom: 0,
                            child: GestureDetector(
                              onTap: (){
                                getCode();
                              },
                              child: Container(
                                width: ScreenUtil().setWidth(180),
                                margin: EdgeInsets.only(bottom: ScreenUtil().setHeight(15),),
                                child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                                  children: <Widget>[
                                    Container(
                                      width: ScreenUtil().setWidth(2),
                                      height: ScreenUtil().setHeight(50),
                                    ),
                                    Text(countdownTime>0?"${countdownTime}s":'register_hq_yzm'.tr(),style: TextStyle(
                                      fontSize: ScreenUtil().setSp(30),
                                    ),)
                                  ],
                                ),
                              ),
                            )
                        )
                      ],
                    )
                ),

                Padding(
                  child: SizedBox(
                    width: ScreenUtil().setWidth(686),
                    height: ScreenUtil().setHeight(80),
                    child: RaisedButton(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(4),
                      ),
                      padding: EdgeInsets.all(0.0),
                      child: Text('submit'.tr(),style: TextStyle(color: Colors.white),),
                      onPressed: () {
                        Future.delayed(Duration(milliseconds: Config.delayedTime), (){
                          _submit();
                        });
                      },
                      elevation: 4.0,
                      color: Theme.of(context).accentColor,
                    ),
                  ),
                  padding: EdgeInsets.only(top: ScreenUtil().setHeight(80),bottom: ScreenUtil().setHeight(60)),
                )
              ],
            ),
            padding: EdgeInsets.only(left: ScreenUtil().setWidth(32),right: ScreenUtil().setWidth(32)),
          ),
        )
    );
  }
}
